package com.application.DataProvider;

import com.application.DTO.UpdateListDTO;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class UpdateListDAO{

	Context _mcontext = null;

	public UpdateListDAO(Context context){
		this._mcontext = context;
	}

	public int getIDMax()
	{
		int idMax = 0;
		DataProvider provider = new DataProvider(_mcontext);
		SQLiteDatabase db = provider.openDataBase();
		Cursor cur = null;
		try 
		{
			String sql = "Select max(id) from UpdateList";
			cur = provider.execQuery(db, sql);
			while (cur.moveToNext()) {
				Log.d("PEERDROID","getString(0): " + cur.getString(0));
				if(cur.getString(0) == null)
					idMax = 0;
				else 
					idMax = cur.getInt(0);
				Log.d("PEERDROID","idMAx: " + idMax);
			}
		} catch (Exception e) {
			// TODO: handle exception
		} finally{
			cur.close();
			db.close();
		}

		return idMax;
	}
	
	public boolean isExist(int id)
	{
		DataProvider provider = new DataProvider(_mcontext);
		SQLiteDatabase db = provider.openDataBase();
		boolean result = false;
		Cursor cur = null;
		try 
		{
			String sql = "Select count(*) from UpdateList where ID=" +  id;
			cur = provider.execQuery(db, sql);
			while (cur.moveToNext()) {
				int i = Integer.valueOf(cur.getString(0));
				if(i != 0)
					result = true;
			}
		} catch (Exception e) {
			// TODO: handle exception
		} finally{
			cur.close();
			db.close();
		}
		return result;
	}
	
//	public String GetUpdateListToDay(int idClass)
//	{
//		DataProvider provider = new DataProvider(_mcontext);
//		SQLiteDatabase db = provider.openDataBase();
//		Cursor cur = null;
//		String s = null;
//		try 
//		{
//			String sql = "select * from UpdateList where idclass = "+idClass+" and datediff(day, getdate(), datelearning)=0 and IDFeelType is not null order by idpart";
//			cur = provider.execQuery(db, sql);
//			while (cur.moveToNext()) {
//				if(s != null)
//					s+= ReadWriteFileController1.getSeparator1();
//				s += cur.getString(0) + ReadWriteFileController1.getSeparator()+ cur.getString(2) + ReadWriteFileController1.getSeparator() + 
//				cur.getString(1) + ReadWriteFileController1.getSeparator() + cur.getString(3) + ReadWriteFileController1.getSeparator() + 
//				cur.getString(4);
//			}
//		} catch (Exception e) {
//			// TODO: handle exception
//		} finally{
//			cur.close();
//			db.close();
//		}
//		return s;
//	}
	
	public UpdateListDTO getUpdateListID(int ID)
	{
		UpdateListDTO ulDto = new UpdateListDTO();
		DataProvider provider = new DataProvider(_mcontext);
		SQLiteDatabase db = provider.openDataBase();
		Cursor cur = null;
		try 
		{
			String sql = "Select * from UpdateList where ID = " + ID;
			cur = provider.execQuery(db, sql);
			while (cur.moveToNext()) {
				ulDto.set_ID(cur.getInt(0));
				ulDto.set_IDTypeTopic(cur.getInt(1));
				ulDto.set_IDTopic(cur.getInt(2));
				ulDto.set_IDUpdateType(cur.getInt(3));
				ulDto.set_Content(cur.getString(4));
				if(cur.getInt(5) == 1)
					ulDto.set_Active(true);
				else
					ulDto.set_Active(false);
			}
		} catch (Exception e) {
			// TODO: handle exception
		} finally{
			cur.close();
			db.close();
		}

		return ulDto;
	}

	public UpdateListDTO getUpdateListID_Teacher(int ID)
	{
		UpdateListDTO ulDto = new UpdateListDTO();
		DataProvider provider = new DataProvider(_mcontext);
		SQLiteDatabase db = provider.openDataBase();
		Cursor cur = null;
		try 
		{
			String sql = "Select * from UpdateList where ID = " + ID;
			cur = provider.execQuery(db, sql);
			while (cur.moveToNext()) {
				ulDto.set_ID(cur.getInt(0));
				ulDto.set_IDTypeTopic(cur.getInt(1));
				ulDto.set_IDTopic(cur.getInt(2));
				ulDto.set_IDUpdateType(cur.getInt(3));
				ulDto.set_Content(cur.getString(4));
			}
		} catch (Exception e) {
			// TODO: handle exception
		} finally{
			cur.close();
			db.close();
		}

		return ulDto;
	}
	public UpdateListDTO getUpdateListID_Topic_Teacher(int IDTopic, int IDTypeTopic)
	{
		UpdateListDTO ulDto = new UpdateListDTO();
		DataProvider provider = new DataProvider(_mcontext);
		SQLiteDatabase db = provider.openDataBase();
		Cursor cur = null;
		try 
		{
			String sql = "Select * from UpdateList where IDTopic = " + IDTopic + " IDTypeTopic = " + IDTypeTopic;
			cur = provider.execQuery(db, sql);
			while (cur.moveToNext()) {
				ulDto.set_ID(cur.getInt(0));
				ulDto.set_IDTypeTopic(cur.getInt(1));
				ulDto.set_IDTopic(cur.getInt(2));
				ulDto.set_IDUpdateType(cur.getInt(3));
				ulDto.set_Content(cur.getString(4));
				
			}
		} catch (Exception e) {
			// TODO: handle exception
		} finally{
			cur.close();
			db.close();
		}

		return ulDto;
	}
	
	public UpdateListDTO getUpdateListID_Topic(int IDTopic, int IDTypeTopic)
	{
		UpdateListDTO ulDto = new UpdateListDTO();
		DataProvider provider = new DataProvider(_mcontext);
		SQLiteDatabase db = provider.openDataBase();
		Cursor cur = null;
		try 
		{
			String sql = "Select * from UpdateList where IDTopic = " + IDTopic + " IDTypeTopic = " + IDTypeTopic;
			cur = provider.execQuery(db, sql);
			while (cur.moveToNext()) {
				ulDto.set_ID(cur.getInt(0));
				ulDto.set_IDTypeTopic(cur.getInt(1));
				ulDto.set_IDTopic(cur.getInt(2));
				ulDto.set_IDUpdateType(cur.getInt(3));
				ulDto.set_Content(cur.getString(4));
				if(cur.getInt(5) == 1)
					ulDto.set_Active(true);
				else
					ulDto.set_Active(false);
			}
		} catch (Exception e) {
			// TODO: handle exception
		} finally{
			cur.close();
			db.close();
		}

		return ulDto;
	}
	public boolean getActive(int IDTopic, int IDTypeTopic)
	{
		boolean result = false;
		DataProvider provider = new DataProvider(_mcontext);
		SQLiteDatabase db = provider.openDataBase();
		Cursor cur = null;
		try 
		{
			String sql = "Select Active from UpdateList where IDTopic = " + IDTopic + " IDTypeTopic = " + IDTypeTopic;
			cur = provider.execQuery(db, sql);
			while (cur.moveToNext()) {
				if(cur.getInt(0) == 1)
					result = true;
			}
		} catch (Exception e) {
			// TODO: handle exception
		} finally{
			cur.close();
			db.close();
		}

		return result;
	}
	public boolean getActive(int id)
	{
		boolean result = false;
		DataProvider provider = new DataProvider(_mcontext);
		SQLiteDatabase db = provider.openDataBase();
		Cursor cur = null;
		try 
		{
			String sql = "Select Active from UpdateList where ID="+id;
			cur = provider.execQuery(db, sql);
			while (cur.moveToNext()) {
				if(cur.getInt(0) == 1)
					result = true;
			}
		} catch (Exception e) {
			// TODO: handle exception
		} finally{
			cur.close();
			db.close();
		}

		return result;
	}
	public String getContent(int IDTopic, int IDTypeTopic)
	{
		String result = "";
		DataProvider provider = new DataProvider(_mcontext);
		SQLiteDatabase db = provider.openDataBase();
		Cursor cur = null;
		try 
		{
			String sql = "Select Content from UpdateList where IDTopic = " + IDTopic + " IDTypeTopic = " + IDTypeTopic;
			cur = provider.execQuery(db, sql);
			while (cur.moveToNext()) {
				result = cur.getString(0);
			}
		} catch (Exception e) {
			// TODO: handle exception
		} finally{
			cur.close();
			db.close();
		}

		return result;
	}
		//Insert du lieu vao bang UpdateList
	public int insertUpdateList_Teacher(UpdateListDTO ulDto) {
		int result = 0;
		DataProvider provider = new DataProvider(_mcontext);
		SQLiteDatabase db = provider.openDataBase();
		try {
			ContentValues values = new ContentValues();
			//int id = getIDMax();
			values.put("ID", ulDto.get_ID());
			values.put("IDTypeTopic", ulDto.get_IDTypeTopic());
			values.put("IDTopic", ulDto.get_IDTopic());
			values.put("IDUpdateType", ulDto.get_IDUpdateType());
			values.put("Content", ulDto.get_Content());
			
			result = provider.execInsertCommand(db, "UpdateList", values);
		} catch (Exception e) {
			// TODO: handle exception
		} finally {
			db.close();
		}

		return result;
	}

	//Insert du lieu vao bang UpdateList
	public int insertUpdateList(UpdateListDTO ulDto) {
		int result = 0;
		DataProvider provider = new DataProvider(_mcontext);
		SQLiteDatabase db = provider.openDataBase();
		try {
			ContentValues values = new ContentValues();
			//int id = getIDMax();
			values.put("ID", ulDto.get_ID());
			values.put("IDTypeTopic", ulDto.get_IDTypeTopic());
			values.put("IDTopic", ulDto.get_IDTopic());
			values.put("IDUpdateType", ulDto.get_IDUpdateType());
			values.put("Content", ulDto.get_Content());
			values.put("Active", ulDto.is_Active());
			result = provider.execInsertCommand(db, "UpdateList", values);
		} catch (Exception e) {
			// TODO: handle exception
		} finally {
			db.close();
		}

		return result;
	}
	//Insert du lieu vao bang UpdateList
	public int insertUpdateList(int id, int idTypeTopic, int idTopic, int idUpdateType, String content, boolean active) {
		int result = 0;
		DataProvider provider = new DataProvider(_mcontext);
		SQLiteDatabase db = provider.openDataBase();
		try {
			ContentValues values = new ContentValues();
			//int id = getIDMax();
			values.put("ID", id);
			values.put("IDTypeTopic", idTypeTopic);
			values.put("IDTopic", idTopic);
			values.put("IDUpdateType", idUpdateType);
			values.put("Content", content);
			values.put("Active", active);
			result = provider.execInsertCommand(db, "UpdateList", values);
		} catch (Exception e) {
			// TODO: handle exception
		} finally {
			db.close();
		}

		return result;
	}
	//Cap nhat bang UpdateList
	public int updateUpdateList(UpdateListDTO ulDto)
	{
		int result=0;
		DataProvider provider = new DataProvider(_mcontext);
		SQLiteDatabase db = provider.openDataBase();
		try{
			ContentValues values = new ContentValues();
			values.put("IDTypeTopic", ulDto.get_IDTypeTopic());
			values.put("IDTopic", ulDto.get_IDTopic());
			values.put("IDUpdateType", ulDto.get_IDUpdateType());
			values.put("Content", ulDto.get_Content());
			values.put("Active", ulDto.is_Active());
			
			String whereClause="ID=?";
			String[]whereArgs = {String.valueOf(ulDto.get_ID())};
			result= provider.execUpdateCommand(db, "UpdateList", values, whereClause, whereArgs);

		} catch (Exception e) {
			// TODO: handle exception
		}finally {
			db.close();
		}
		return result;
	}

	public int updateActive(int idUpdatelist, boolean isActive)
	{
		int result=0;
		DataProvider provider = new DataProvider(_mcontext);
		SQLiteDatabase db = provider.openDataBase();
		try{
			ContentValues values = new ContentValues();
			values.put("Active", isActive);
			
			String whereClause="ID=?";
			String[]whereArgs = {String.valueOf(idUpdatelist)};
			result= provider.execUpdateCommand(db, "UpdateList", values, whereClause, whereArgs);

		} catch (Exception e) {
			// TODO: handle exception
		}finally {
			db.close();
		}
		return result;
	}
	
	public int updateContent(int idUpdatelist, String content)
	{
		int result=0;
		DataProvider provider = new DataProvider(_mcontext);
		SQLiteDatabase db = provider.openDataBase();
		try{
			ContentValues values = new ContentValues();
			values.put("Content", content);
			
			String whereClause="ID=?";
			String[]whereArgs = {String.valueOf(idUpdatelist)};
			result= provider.execUpdateCommand(db, "UpdateList", values, whereClause, whereArgs);

		} catch (Exception e) {
			// TODO: handle exception
		}finally {
			db.close();
		}
		return result;
	}
	//Cap nhat bang UpdateList
	public int updateUpdateList_Teacher(UpdateListDTO ulDto)
	{
		int result=0;
		DataProvider provider = new DataProvider(_mcontext);
		SQLiteDatabase db = provider.openDataBase();
		try{
			ContentValues values = new ContentValues();
			values.put("IDTypeTopic", ulDto.get_IDTypeTopic());
			values.put("IDTopic", ulDto.get_IDTopic());
			values.put("IDUpdateType", ulDto.get_IDUpdateType());
			values.put("Content", ulDto.get_Content());
			
			String whereClause="ID=?";
			String[]whereArgs = {String.valueOf(ulDto.get_ID())};
			result= provider.execUpdateCommand(db, "UpdateList", values, whereClause, whereArgs);

		} catch (Exception e) {
			// TODO: handle exception
		}finally {
			db.close();
		}
		return result;
	}
	
	//Delete doi tuong UpdateList
	public int deleteUpdateList(int id)
	{
		int result=0;
		DataProvider provider = new DataProvider(_mcontext);
		SQLiteDatabase db = provider.openDataBase();
		try{	
			String whereClause="ID=?";
			String[]whereArgs = {String.valueOf(id)};	
			result=provider.execDeleteCommand(db, "UpdateList", whereClause, whereArgs);
		} catch (Exception e) {
			// TODO: handle exception
		}finally {
			db.close();
		}
		return result;
	}
	
	//Delete doi tuong UpdateList
	public int deleteUpdateList(int idTopic, int idTopicType)
	{
		int result=0;
		DataProvider provider = new DataProvider(_mcontext);
		SQLiteDatabase db = provider.openDataBase();
		try{	
			String whereClause="IDTopic=? and IDTypeTopic=?";
			String[]whereArgs = {String.valueOf(idTopic),String.valueOf(idTopicType)};	
			result=provider.execDeleteCommand(db, "UpdateList", whereClause, whereArgs);
		} catch (Exception e) {
			// TODO: handle exception
		}finally {
			db.close();
		}
		return result;
	}

}
